package com.dhh.activity;

import java.util.Calendar;

import greendroid.app.GDActivity;
import greendroid.widget.ActionBarItem.Type;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import com.dhh.R;
import com.dhh.dao.DataBaseHelper;
import com.dhh.util.CUtil;
import com.dhh.util.WeightInfo;
import com.dhh.util.manager.GoalManager;
import com.dhh.util.manager.PlanManager;
import com.dhh.util.manager.WeightManager;
import com.dhh.vo.Goal;
import com.dhh.vo.Weight;
import com.dhh.widget.CustomMenu.OnMenuItemSelectedListener;
import com.dhh.widget.CustomMenuItem;

public class GoalActivity extends GDActivity implements OnMenuItemSelectedListener,View.OnClickListener {
	private Goal goal;
	private DataBaseHelper db;
	private GoalManager gManager;
	private PlanManager pManager;
	private WeightManager wManager;
	private Button targetWeight;
	private Button wayGain;
	private Button gender;
	private Button age;
	private Button height;
	private Button weight;
	private Button activityLevel;
	private Button addGoal;
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        
        db = new DataBaseHelper(getBaseContext());
        db.openDataBase();
        gManager = new GoalManager(db);
        pManager = new PlanManager(db); 
        wManager = new WeightManager(db);
        
        setActionBarContentView(R.layout.my_goal);
        getActionBar().setTitle("My Goal");
        getActionBar().setBackgroundColor(Color.rgb(14, 166, 249));
        getActionBar().getChildAt(0).
	    	setOnClickListener(new OnClickListener(){
				@Override
				public void onClick(View v) {
					Intent intent = new Intent(GoalActivity.this,HomeActivity.class);	
					startActivity(intent);
				}}); 
        addActionBarItem(Type.Help).getItemView().
    	setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(GoalActivity.this,SelectTopicActivity.class);	
				intent.putExtra("food_group", "activity_level");
				startActivity(intent);
			}}); 

		targetWeight = (Button) this.findViewById(R.id.goals_target_weight);
		wayGain = (Button) this.findViewById(R.id.goals_way_gain);
		gender = (Button) this.findViewById(R.id.goals_gender);
		age = (Button) this.findViewById(R.id.goals_age);
		height = (Button) this.findViewById(R.id.goals_height);
		weight = (Button) this.findViewById(R.id.goals_weight);
		activityLevel = (Button) this.findViewById(R.id.goals_level);		
		addGoal = (Button) this.findViewById(R.id.goals_plan);	
		
		targetWeight.setOnClickListener(this);
		wayGain.setOnClickListener(this);
		gender.setOnClickListener(this);
		age.setOnClickListener(this);	
		height.setOnClickListener(this);
		weight.setOnClickListener(this);
		activityLevel.setOnClickListener(this);
		addGoal.setOnClickListener(this);       
        
              
	}
	
	@Override
	public void onStart() {
        super.onStart();
        initGoal();  
	}
	
	public void initGoal(){
		goal = gManager.getCurrentGoal();
		if(goal.getId()==null){
			getActionBar().removeViewAt(0);
		}
		
        fillData();
		addGoal.setVisibility(View.INVISIBLE);
	}
	
	public void fillData(){	
		if(goal==null)
			return;
		if(goal.getTargetWeight()!=null && goal.getTargetWeight().length()>0)
			targetWeight.setText(goal.getTargetWeight()+" lbs");
		if(goal.getWayGain()!=null && goal.getWayGain().length()>0)
			wayGain.setText("Gain "+goal.getWayGain()+" lbs/week");	
		if(goal.getGender()!=null && goal.getGender().length()>0)	
			gender.setText(goal.getGender());
		if(goal.getAge()!=null && goal.getAge().length()>0)
			age.setText(goal.getAge()+" yrs. old");
		if(goal.getHeight()!=null && goal.getHeight().length()>0)
			height.setText(goal.getHeight()+" \"");
		if(goal.getWeight()!=null && goal.getWeight().length()>0)
			weight.setText(goal.getWeight()+" lbs");
		if(goal.getActivityLevel()!=null && goal.getActivityLevel().length()>0)
			activityLevel.setText(goal.getActivityLevel());

		checkShowAddButton();
	}

	@Override
	public void MenuItemSelectedEvent(CustomMenuItem selection) {		
	}
	
	int selectedItem = 0;
	private void showSelectDialog(String title,String value,final String items[],final Goal goal,final View v){
		if(value==null || value.trim().length()==0)
			value = "0";
		for(int i=0; i<items.length; i++){
			if(v.getId()==R.id.goals_target_weight || v.getId()==R.id.goals_weight){
				if(Double.parseDouble(value)==Double.parseDouble(items[i])){
					selectedItem = i;
					break;
				}
			}else if(v.getId()==R.id.goals_way_gain){
				if(("Gain "+value+" lbs/week").equals(items[i])){
					selectedItem = i;
					break;
				}else{
					selectedItem = 0;
				}
			}else if(v.getId()==R.id.goals_gender){
				if(value.equals(items[i])){
					selectedItem = i;
					break;
				}else{
					selectedItem = 0;
				}
			}else if(v.getId()==R.id.goals_height){
				if((value+"\"").equals(items[i])){
					selectedItem = i;
					break;
				}else{
					selectedItem = 0;
				}
			}else if(v.getId()==R.id.goals_age){
				if((value+" yrs. old").equals(items[i])){
					selectedItem = i;
					break;
				}else{
					selectedItem = 0;
				}
			}else if(v.getId()==R.id.goals_level){
				if(value.equals(items[i])){
					selectedItem = i;
					break;
				}else{
					selectedItem = 0;
				}
			}
		}
		
		AlertDialog.Builder ab = new AlertDialog.Builder(GoalActivity.this);
		ab.setTitle(title);		
		ab.setSingleChoiceItems(items, selectedItem,
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int whichButton) {
						selectedItem = whichButton;
					}
				})
				.setPositiveButton("OK", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int whichButton) {
						if(v.getId()==R.id.goals_target_weight){
							goal.setTargetWeight(items[selectedItem]);
						}else if(v.getId()==R.id.goals_way_gain){
							goal.setWayGain(WeightInfo.wayGainOptions[selectedItem]);
						}else if(v.getId()==R.id.goals_gender){
							goal.setGender(items[selectedItem]);
						}else if(v.getId()==R.id.goals_height){
							goal.setHeight(WeightInfo.heightOptions[selectedItem]+"");
						}else if(v.getId()==R.id.goals_weight){
							goal.setWeight(items[selectedItem]);
						}else if(v.getId()==R.id.goals_age){
							goal.setAge(WeightInfo.ageOptions[selectedItem]);
						}else if(v.getId()==R.id.goals_level){
							goal.setActivityLevel(items[selectedItem]);
						}
						
						fillData();
						
					}
				})
				.setNegativeButton("Cancel",
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog,
									int whichButton) {
							}
						});
		ab.show();
	}

	@Override
	public void onClick(View v) {
		if(v.getId() == R.id.goals_target_weight){
			showSelectDialog("Target Weight",goal.getTargetWeight(),WeightInfo.targetWeightOptions,goal,v);
		}else if(v.getId() == R.id.goals_way_gain){
			showSelectDialog("How to Gain",goal.getWayGain(),WeightInfo.wayGainOptionsV,goal,v);
		}else if(v.getId() == R.id.goals_age){
			showSelectDialog("Age",goal.getAge(),WeightInfo.ageOptionsV,goal,v);
		}else if(v.getId() == R.id.goals_gender){
			showSelectDialog("Gender",goal.getGender(),WeightInfo.genders,goal,v);
		}else if(v.getId() == R.id.goals_height){
			showSelectDialog("Height",goal.getHeight(),WeightInfo.heightOptionsV,goal,v);
		}else if(v.getId() == R.id.goals_level){
			showSelectDialog("Activity Level",goal.getActivityLevel(),WeightInfo.acitivityLevelOptions,goal,v);
		}else if(v.getId() == R.id.goals_weight){
			showSelectDialog("Current Weight",goal.getWeight(),WeightInfo.targetWeightOptions,goal,v);
		}else if(v.getId() == R.id.goals_plan){
//			saveToPlan();
//			Intent myIntent = new Intent(GoalActivity.this, FoodPlanActivity.class);
//			myIntent.putExtra("goalId", goal.getId());
//			this.startActivity(myIntent);
			confirmChage();
		}
		
	}
	
	private void confirmChage(){
		new AlertDialog.Builder(this)
        .setIcon(android.R.drawable.ic_dialog_alert)
        .setTitle("Change Alert")
        .setMessage("After change the goal, the food plan also will be changed. Are you sure?")
        .setPositiveButton("OK", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
            	saveToPlan();
    			Intent myIntent = new Intent(GoalActivity.this, FoodPlanActivity.class);
    			myIntent.putExtra("goalId", goal.getId());
    			GoalActivity.this.startActivity(myIntent);
            	GoalActivity.this.finish();    
            }

        })
        .setNegativeButton("Cancle", null)
        .show();
	}
	
	@Override
    public void onResume(){
    	super.onResume();
        db.openDataBase();       
    }
    
    @Override
	protected void onPause(){
		super.onPause();
		if (db != null) {
			db.close();
		}
	}
    
    @Override
	protected void onStop(){
		super.onStop();
	}
	
	private void saveToPlan(){
		gManager.addGoal(goal);
		Weight w = new Weight();
		w.setDate(CUtil.getDate(Calendar.getInstance().getTime()));
		w.setWeight(goal.getWeight());
		wManager.addWeight(w);
		goal = gManager.getCurrentGoal();
		pManager.addPlan(goal);
	}
	
	private void  checkShowAddButton(){
		if(goal!=null && 
		   goal.getActivityLevel()!=null &&
		   goal.getAge()!=null &&
		   goal.getGender()!=null &&
		   goal.getHeight()!=null &&
		   goal.getTargetWeight()!=null &&
		   goal.getWayGain()!=null &&
		   goal.getWeight()!=null){
			addGoal.setVisibility(View.VISIBLE);
		}else{
			addGoal.setVisibility(View.INVISIBLE);
		}			
	}
	

}
